/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.service;

import br.com.foxline.model.ActCep;
import br.com.foxline.model.Quality;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author roniere
 */
@Stateless
@LocalBean
public class QualityService extends GenericService<Quality> {

    public QualityService() {
        super(Quality.class);
    }

    public List<Quality> autoComplete(String str) {
        String sql = "Select q from Quality q"
                + " where lower(q.description) like lower(:desc)";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("desc", str + "%");
        return (List<Quality>) query.getResultList();
    }
    
    public List<Quality> findQuality(Quality quality) {
        String sql = "Select q from Quality q";
        if(quality.getCod()!=null)
                sql += " where q.cod = :cod ";
        if(quality.getDescription()!=null)
            sql += "or lower(q.description) like lower(:desc)";
        Query query = getEntityManager().createQuery(sql);
        if(quality.getCod()!=null)
            query.setParameter("cod", quality.getCod());
        if(quality.getDescription()!=null)
            query.setParameter("desc", "%"+quality.getDescription()+"%");
        System.err.println("ss"+quality.getCod() + "" +quality.getDescription());
        return (List<Quality>) query.getResultList();
    }
    
    
    public Quality findQualityCod(String Cod) {
        Quality quality; 
        String sql = "Select q from Quality q Where  q.cod like lower(:cod) ";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("cod", Cod);
        try {
            quality = (Quality) query.getSingleResult();
        } catch (NoResultException nr) {
            return null;
        } catch (NonUniqueResultException nre) {
            return null;
        }
        return quality;
    }
}
